package com.example.service.impl;


import com.example.mapper.InheritorMapper;
import com.example.pojo.Inheritor;
import com.example.service.InheritorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class InheritorServiceImpl implements InheritorService {

    @Autowired
    private InheritorMapper inheritorMapper;


    //获取所有传承人
    @Override
    public List<Inheritor> getAllInheritors() {
        return inheritorMapper.findAll();
    }

    //根据区域和物种获取传承人
    @Override
    public List<Inheritor> findAllByCategory(Integer regionId, Integer speciesId) {
        return inheritorMapper.findAllByCategory(regionId, speciesId);
    }

    //根据传承人id获取传承人信息
    @Override
    public Inheritor getInheritorById(Integer id) {
        return inheritorMapper.findById(id);
    }

    //根据非遗项目id获取该项目的所有传承人
    @Override
    public List<Inheritor> getPersonsByHeritageId(Integer heritageId) {
        return inheritorMapper.findPersonsByProjectId(heritageId);
    }

    //增加传承人
    @Override
    public void insertInheritor(Inheritor inheritor) {
        Integer regionId = inheritorMapper.findRegionIdByName(inheritor.getRegionName());
        Integer speciesId = inheritorMapper.findSpeciesIdByName(inheritor.getSpeciesName());
        inheritor.setRegionId(regionId);
        inheritor.setSpeciesId(speciesId);
        Integer projectId = inheritorMapper.findProjectIdByProjectName(inheritor.getProjectName()) ;
        inheritor.setProjectId(projectId);
        inheritorMapper.insertInheritor(inheritor);
        Integer inheritorId = inheritor.getId();

        inheritorMapper.insertInherit(inheritorId, projectId);
    }

    @Override
    public void deleteInheritorById(Integer id) {
        inheritorMapper.deleteInheritorById(id);
    }

    @Override
    public void updateInheritor(Inheritor inheritor) {
        Integer regionId = inheritorMapper.findRegionIdByName(inheritor.getRegionName());
        Integer speciesId = inheritorMapper.findSpeciesIdByName(inheritor.getSpeciesName());
        Integer projectId = inheritorMapper.findProjectIdByProjectName(inheritor.getProjectName());

        inheritor.setRegionId(regionId);
        inheritor.setSpeciesId(speciesId);
        inheritor.setProjectId(projectId);
        inheritorMapper.updateInheritor(inheritor);
    }


}
